package Gui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle.ComponentPlacement;

import Control.UserSubscriber;
import Logic.LoginInfo;
import client.LibaryMain;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.border.LineBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
/**
 * this class implements gui lending card 
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */
public class LendingCard extends JPanel {
        private JTable table;
        private JTable table_3;
        private JTabbedPane tabbedPane;
        private JButton btnBack;
        private JButton btnLogout;
        private JButton btnExtendLending;
        private JTable table_1;
        String bookid,subid;
/**
 * Constructor initilize the lending card window
 * @param str
 */
        public  LendingCard(String[] str){
                
                initComponents(str);
                MainScreen.card.add("LendingCard", this);
                setLayout(null);
                add(tabbedPane);
                add(btnBack);
                add(btnLogout);
                add(btnExtendLending);
                
                JLabel label = new JLabel("");
                label.setIcon(new ImageIcon(LendingCard.class.getResource("/Gui/images/graphite-user.png")));
                label.setBounds(219, 211, 743, 477);
                add(label);
                MainScreen.cardLayot.show(MainScreen.card, "LendingCard");
}

        
        /**
    	 * this function create instance of Lending card gui 
    	 */
        
        public void  initComponents(String[] str) {
                
                setBackground(new Color(153, 204, 255));
                
                btnLogout = new JButton("Logout");
                btnLogout.setBounds(850, 551, 83, 25);
                btnLogout.setFont(new Font("Tahoma", Font.BOLD, 14));
                btnLogout.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent arg0) {
                                ArrayList<String> Arr=new ArrayList<String>();
                                Arr.add("Logout");
                                Arr.add("Subscriber");
                                Arr.add(LoginInfo.UserName);
                                LibaryMain.chat.handleMessageFromGui(Arr);
                                        MainScreen.cardLayot.show(MainScreen.card, "Login");
                                        
                                }
                        });
                btnBack = new JButton("Back");
                btnBack.setBounds(77, 551, 65, 25);
                btnBack.setFont(new Font("Tahoma", Font.BOLD, 14));
                btnBack.addActionListener(new java.awt.event.ActionListener() {     
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            
            }

                                        private void btnBackActionPerformed(ActionEvent evt) {
                                                
                                                MainScreen.cardLayot.show(MainScreen.card, "SubscriberWindow");
                                                
                                        }
                                
        });
                
                tabbedPane = new JTabbedPane(JTabbedPane.TOP);
                tabbedPane.setBounds(27, 35, 963, 186);
                tabbedPane.setFont(new Font("Tahoma", Font.PLAIN, 20));
                
                btnExtendLending = new JButton("Extend lending");
                btnExtendLending.setBounds(734, 246, 196, 54);
                btnExtendLending.setFont(new Font("Tahoma", Font.BOLD, 14));
                btnExtendLending.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                            ArrayList<String> Arr=new ArrayList<String>();
                            Arr.add("ExtendLending");
                            Arr.add(bookid);
                            Arr.add(LoginInfo.UserName);
                            LibaryMain.chat.handleMessageFromGui(Arr);
                            
                            
                    }
            });
                
                
                Vector<Vector<Object>> data = new Vector<Vector<Object>>();
                Vector<Vector<Object>> data2 = new Vector<Vector<Object>>();
                Vector<Vector<Object>> data3 = new Vector<Vector<Object>>();
                        int newcount = Integer.parseInt(str[1]);
                        newcount=newcount*3;
                for (int i=3; i<=(newcount);i=i+3){
                Vector<Object> row = creatrow(str,i);
                data.add(row);
                }
                
                Vector<String> headers = new Vector<String>();
                     headers.add("Copy ID");
                     headers.add("Title");
                     headers.add("Return Date");
                final JTable table = new JTable( data, headers)
                {
                                /**
                                 * 
                                 */
                                private static final long serialVersionUID = 1L;

                                public boolean isCellEditable(int data, int columns)
                       {
                           if(columns>=0)
                             return false;
                           else return true;
                       }
                };
                table.setFont(new Font("Tahoma", Font.PLAIN, 16));
                
                
                int newcount2 = Integer.parseInt(str[1]);
                 newcount2=newcount2*3+3;
                 int newcount3 = Integer.parseInt(str[2]);
                 newcount3=newcount3*3+newcount2;
                for (int i=newcount2; i<=newcount3-3;i=i+3){
                        Vector<Object> row2 = creatrow(str,i);
                        data2.add(row2);
                        }
                
                      Vector<String> headers2 = new Vector<String>();
                     headers2.add("Title");
                     headers2.add("Planned return date");
                     headers2.add("Actual return date");  
                     
                     for (int i=newcount3; i<=str.length-3;i=i+3){
                                Vector<Object> row3 = creatrow(str,i);
                                data3.add(row3);
                                }
                        
                              Vector<String> headers3 = new Vector<String>();
                             headers3.add("Copy ID");
                             headers3.add("Problem");
                             headers3.add("Problem date");  
                             
                             final JTable table_3 = new JTable( data2, headers2)  
                             {
                                 /**
                                  * 
                                  */
                                 private static final long serialVersionUID = 1L;

                                 public boolean isCellEditable(int data, int columns)
                        {
                            if(columns>=0)
                              return false;
                            else return true;
                        }
                 };
                             final JTable table_1 = new JTable( data3, headers3)
                            
                             {
                                 /**
                                  * 
                                  */
                                 private static final long serialVersionUID = 1L;

                                 public boolean isCellEditable(int data, int columns)
                        {
                            if(columns>=0)
                              return false;
                            else return true;
                        }
                 };
                             table_1.setFont(new Font("Tahoma", Font.PLAIN, 16));
                     table.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
                     table_3.setFont(new Font("Tahoma", Font.PLAIN, 16));
                     
                     JScrollPane scrollPane2 = new JScrollPane(table_1);
                                
                                
                        //      table_1 = new JTable();
                                scrollPane2.setViewportView(table_1);
                                
                     
                     JScrollPane pane2 = new JScrollPane(table);
                     JScrollPane scrollPane = new JScrollPane(table_3); 
                 tabbedPane.addTab("  Current lending  ", pane2);
                     tabbedPane.addTab("  History  ", null, scrollPane, null);
                     tabbedPane.addTab("Problems", null, scrollPane2, null);
                     
                     table.addMouseListener(new MouseAdapter() {
                         @Override
                         public void mouseClicked(MouseEvent arg0) {
                                 
                                 int col = table.getSelectedColumn();   
                                 int row = table.getSelectedRow();
                         
                                 boolean is=table.isColumnSelected(0);
                         
                                 if (!is){
                                 
                                         System.out.println("wrong selection");
                                         JOptionPane.showMessageDialog(null, "Please Select only Book ID");

                                 }
                                 else{   Object o=((String)table.getValueAt(row, col));
                                                 bookid=o.toString();
                                                 System.out.println(""+o);
                                                 System.out.println("bookid:"+bookid);
                                 }
                                 
                                 
                                 
                                 
                         }
                 });
        }
        private Vector<Object> creatrow(String[] str, int i) {
                 Vector<Object> row = new Vector<Object>();
                 row.add( ""+str[i]);
                 row.add( ""+str[i+1]);
                 row.add( ""+str[i+2]);
                return row;
        }
}

